package com.hxd.otc.utils.download;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.hxd.otc.common.SpConfig;
import com.hxd.otc.utils.SpUtil;


/**
 * Created by lzy on 2018/7/2 0002.
 * description ${系统下载成功广播}
 */
public class DownloadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            DownloadInfoBean downloadInfoBean = SpUtil.PERMANENT.getObject(SpConfig.Permanent.DOWNLOAD_INFO, DownloadInfoBean.class);
            long cacheDownloadId = downloadInfoBean == null ? 0 : downloadInfoBean.getDownloadId();

            if (cacheDownloadId != 0 && downloadId == cacheDownloadId) {
                DownloadManagerUtil.installApk(downloadInfoBean.getSaveName());
            }
        }
    }
}
